/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.EditorMenu;

import B3D_Elements.B3D_Element;
import Files.Project;
import GUI.Components.B3DSFileView;
import GUI.Dialogs.CreateProjectDialog;
import GUI.Dialogs.ExitDialog;
import General.CurrentData;
import other.ElementToObjectConverter;
import other.ObjectToElementConverter;
import other.Wizard;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.Map;
import java.util.Vector;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author David
 */
public class FileMenu extends JMenu
{

    private JMenuItem newItem = new JMenuItem("New", new ImageIcon("dat//img//menu//newIcon.png"));
    private JMenuItem openItem = new JMenuItem("Open", new ImageIcon("dat//img//menu//openIcon.png"));
    private JMenuItem openRecentItem = new JMenuItem("Open Recent", new ImageIcon("dat//img//menu//openAsIcon.png"));
    private JMenuItem saveItem = new JMenuItem("Save", new ImageIcon("dat//img//menu//saveIcon.png"));
    private JMenuItem saveAsItem = new JMenuItem("Save as...", new ImageIcon("dat//img//menu//saveAsIcon.png"));
    private JMenuItem quitItem = new JMenuItem("Quit", new ImageIcon("dat//img//menu//quitIcon.png"));

    /**
     *
     */
    public FileMenu()
    {
        initShortcuts();
        setText("File");
        add(newItem);
        add(openItem);
        add(openRecentItem);
        add(saveItem);
        add(saveAsItem);
        add(new JSeparator());
        add(quitItem);
        initActions();
    }

    private void initActions()
    {
        newItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if (CurrentData.getProject() != null)
                {
                    if (JOptionPane.showConfirmDialog(
                            newItem,
                            "By creating a new Project, the current one will be closed an all unsaved data will be lost.\nContinue?",
                            "Warning",
                            JOptionPane.YES_NO_OPTION,
                            JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION)
                    {
                        new CreateProjectDialog();
                        return;
                    }
                } else
                {
                    new CreateProjectDialog();
                }
            }
        });
        openItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                JFileChooser jfc = new JFileChooser();
                jfc.setFileView(new B3DSFileView());
                jfc.setFileFilter(new FileFilter()
                {
                    @Override
                    public boolean accept(File f)
                    {
                        return (f.isDirectory() || f.getAbsolutePath().endsWith(".b3dp"));
                    }

                    @Override
                    public String getDescription()
                    {
                        return "B3DP";
                    }
                });
                jfc.showOpenDialog(openItem);
                CurrentData.setProject((Project) Wizard.loadFile(jfc.getSelectedFile().getAbsolutePath()));
                CurrentData.getProject().getYourShitTogether();
                CurrentData.initProjectScene();
            }
        });
        saveItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                Vector<B3D_Element> allElements = new Vector<B3D_Element>();
                Vector<Object> allObjects = new Vector<Object>();
                for (Map.Entry<Object, B3D_Elements.B3D_Element> entry : CurrentData.getEditorWindow().getB3DApplication().getElementList().getObjectHashMap().entrySet())
                {
                    allElements.add(ObjectToElementConverter.convertToElement(entry.getKey()));
                    allObjects.add(ElementToObjectConverter.converToObject(allElements.lastElement()));
                }
                System.out.println("################");
                CurrentData.getProject().writeFromScene(allElements);
                allElements.clear();
                for (Object object : allObjects)
                {
                    allElements.add(ObjectToElementConverter.convertToElement(object));
                }
                System.out.println("################");
                CurrentData.getProject().writeFromScene(allElements);
                //                CurrentData.getProject().writeFromScene(CurrentData.getEditorWindow().getB3DApplication().getElementList());
//                CurrentData.getProject().save();
            }
        });
        quitItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if (CurrentData.getConfiguration().getSettings().get("exitwithoutprompt").equals("true"))
                {
                    System.exit(0);
                }
                new ExitDialog();
            }
        });
    }

    private void initShortcuts()
    {
        newItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_N, ActionEvent.CTRL_MASK));
        openItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_O, ActionEvent.CTRL_MASK));
        saveItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_S, ActionEvent.CTRL_MASK));
        quitItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
    }
}